package com.leetcode.LeetCode精选TOP面试题;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 覃国强
 * @date 2022-07-04 17:39
 */
public class 题库_0163_缺失的区间 {

  static
  class Solution {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
      List<String> answer = new ArrayList<>();
      if (lower > upper) {
        return answer;
      }
      for (int num : nums) {
        if (num == lower) {
          ++lower;
        } else {
          int next = num - 1;
          addSection(answer, lower, next);
          lower = num + 1;
        }
      }
      if (lower <= upper) {
        addSection(answer, lower, upper);
      }
      return answer;
    }

    private void addSection(List<String> answer, int lower, int upper) {
      if (lower == upper) {
        answer.add(String.valueOf(lower));
      } else {
        answer.add(lower + "->" + upper);
      }
    }
  }

}
